﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Adams.Web.Logic.Security
{
    public class Resend
    {
        public static Adams.Web.Models.Security.Resend.PageLoad PageLoad(string email)
        {
            var model = new Adams.Web.Models.Security.Resend.PageLoad();
            model.Email = email;
            return model;
        }

        public static Models.FormOutput TryResend(Models.Security.Resend.FormInput input)
        {
            var output = Validation.PerformBasicValidation(input);

            if (output.IsValid)
            {
                var data = Data.Security.Common.GetMemberInfo(input.Email); 

                if (data.Exists)
                {
                    // User's email is not confirmed
                    if (data.IsEmailConfirmed)
                    {
                        output.InvalidFields.Add(new Models.InvalidField() { Name = "Email", Code = 28 });
                    }
                    else
                    {
                        string subject = "Confirm your ADAMS account - Resent";
                        string from = Shared.Config.GetAppSetting("SecurityFromEmail");
                        string body = Logic.Email.GenerateSignupEmail(input.Email);
                        Email.QueueEmail(subject, from, input.Email, body, 1);
                    }
                }
                else
                {
                    output.InvalidFields.Add(new Models.InvalidField() { Name = "Email", Code = 20 });
                }
            }

            return output;
        }
    }
}
